﻿using STF.Blog.Dtos;
using STF.Blog.IRepository;
using STF.Blog.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace STF.Blog.Service
{
    public class ClassifyColumnService : IClassifyColumnService
    {
        private IClassifyColumnManager _ic;
        public ClassifyColumnService(IClassifyColumnManager ic)
        {
            _ic = ic;
        }
        public async Task<int> AddClassifyColumnAsync(string title, int uid)
        {
            return await _ic.AddAsync(new Model.ClassifyColumn 
            {
                Title = title,
                Uid = uid
            });
        }

        public async Task<int> DeleteClassifyColumnAsync(int id)
        {
            var data = await _ic.GetById(id);
            if (data == null)
                return -1;
            if (data.DeleteId != 1)
                return -2;
            return await _ic.DeleteAsync(data);
        }

        public async Task<List<ClassifyColumnDto>> GetAllClassifyColumnAsync(int deleteId = 1)
        {
            var data = await _ic.GetChooseAsync(cl => cl.DeleteId == deleteId);
            return data == null ? new List<ClassifyColumnDto>() : data.Select(cl => new ClassifyColumnDto 
            {
                Id = cl.Id,
                Title = cl.Title,
                Uid = cl.Uid,
                CreateTime = cl.CreateTime,
                UpdateTime = cl.UpdateTime
            }).ToList();
        }

        public async Task<string> GetByCid(string cName)
        {
            var data = await _ic.GetChooseAsync(c => c.Title.Equals(cName));
            return data == null ? null : data[0].Id.ToString();
        }

        public async Task<ClassifyColumnDto> GetByIdAsync(int id)
        {
            var data = await _ic.GetById(id);
            return data == null ? new ClassifyColumnDto() : new ClassifyColumnDto 
            {
                Id = data.Id,
                Title = data.Title,
                Uid = data.Uid,
                DeleteId = data.DeleteId,
                CreateTime = data.CreateTime,
                UpdateTime = data.UpdateTime
            };
        }

        public async Task<List<ClassifyColumnDto>> GetByIdListAsync(int id, int deleteId = 1)
        {
            var data = await _ic.GetChooseAsync(cfy => cfy.Uid == id);
            return data == null ? new List<ClassifyColumnDto>() : data.Select(cl => new ClassifyColumnDto
            {
                Id= cl.Id,
                Title= cl.Title,
                DeleteId = cl.DeleteId,
                Uid = cl.Uid
            }).ToList();
        }

        public async Task<List<ClassifyColumnDto>> GetByNameAsync(string title, int deleteId = 1)
        {
            var data = await _ic.GetChooseAsync(cl => cl.Title.Contains(title) && cl.DeleteId == deleteId);
            return data == null ? new List<ClassifyColumnDto>() : data.Select(cl => new ClassifyColumnDto 
            {
                Id = cl.Id,
                Title = cl.Title,
                Uid = cl.Uid,
                CreateTime = cl.CreateTime,
                UpdateTime = cl.UpdateTime
            }).ToList();
        }

        public async Task<int> PutTrashAsync(int id)
        {
            var data = await _ic.GetById(id);
            if (data == null || data.DeleteId == 0)
                return -1;
            data.DeleteId = 0;
            return await _ic.UpdateAsync(data);
        }

        public async Task<int> RestoreAsync(int id)
        {
            var data = await _ic.GetById(id);
            if (data == null || data.DeleteId == 1)
                return -1;
            data.DeleteId = 1;
            return await _ic.UpdateAsync(data);
        }

        public async Task<int> UpdateClassifyColumnAsync(int id, string title, int uid)
        {
            var data = await _ic.GetById(id);
            if(data == null)
                return 0;
            data.Title = title;
            data.Uid = uid;
            return await _ic.UpdateAsync(data);
        }
    }
}
